package com.app.manage.moudles.app.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.app.manage.moudles.app.entity.TApp;
import com.app.manage.moudles.app.service.TAppService;
import com.app.manage.moudles.confs.constants.GroupEnums;
import com.app.manage.moudles.confs.constants.VariTypeEnums;
import com.app.manage.moudles.confs.dao.TGroupRelationDao;
import com.app.manage.moudles.confs.entity.TGroup;
import com.app.manage.moudles.confs.entity.TVari;
import com.app.manage.moudles.confs.service.TVariService;
import com.app.manage.utils.PageSearch;
import com.app.manage.utils.Rsp;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("app")
@Api(tags = "应用")
public class AppController {

    @Resource
    private TVariService tVariService;

    @Resource
    private TAppService tAppService;

    @Resource
    private TGroupRelationDao groupRelationDao;

    @GetMapping("listAppVarCols")
    @ApiOperation("查询变量")
    public Rsp listAppVarCols(){
        List<TVari>  variList = tVariService.list(new LambdaQueryWrapper<TVari>()
                .eq(TVari::getVariType, VariTypeEnums.APP_VAR.getType())
        );
        List<Map<String,Object>> retList = new ArrayList<>();
        variList.forEach(o->{
            Map<String,Object> map = new HashMap<>();
            map.put("column",o.getVariKey());
            map.put("name",o.getVariName());
            map.put("value",o.getVariValue());
            retList.add(map);
        });
        return Rsp.success(retList);
    }

    @PostMapping("pageList")
    @ApiOperation("分页查询应用")
    public Rsp pageList(@RequestBody PageSearch<TApp> s){
        return Rsp.success(tAppService.pageData(s.getPageNum(),s.getPageSize(),s.getDomain()));
    }

    @GetMapping("listAppByGroup")
    @ApiOperation("查询应用分组")
    public Rsp listAppByGroup(){
        List<TGroup> groupList = groupRelationDao.selectGroupByRelation(GroupEnums.APP_GROUP.getType());
        Map<Integer,List<TGroup>> groupIdMap = groupList.stream().collect(Collectors.groupingBy(TGroup::getId));


        List<TApp> appList = tAppService.list();
        Map<Integer,TApp> appIdMap = new HashMap<Integer,TApp>(){{
            appList.forEach(o->{
                put(o.getId(),o);
            });
        }};

        List<Map<String,Object>> retList = new ArrayList<>();
        for(Map.Entry<Integer,List<TGroup>> e : groupIdMap.entrySet()) {
            Map<String,Object> map = new HashMap<>();
            map.put("groupName",e.getValue().get(0).getGroupName());
            List<TApp> tempList = new ArrayList<>();
            for (TGroup t : e.getValue()){
                tempList.add(appIdMap.get(t.getRelationId()));
            }
            map.put("appList",tempList);
            retList.add(map);
        }
        return Rsp.success(retList);
    }


    @PostMapping("saveData")
    @ApiOperation("保持应用")
    public Rsp saveData(@RequestBody Map<String,Object> map){
        String res = tAppService.saveData(map);
        if(StringUtils.isNotEmpty(res)){
            return Rsp.fail(res);
        }
        return Rsp.success();
    }

    @PostMapping("remove")
    @ApiOperation("删除应用")
    public Rsp remove(String ids){
        List<String> idList = Arrays.asList(ids.split(","));
        if(CollectionUtil.isEmpty(idList)){
            return Rsp.fail("请选择一条数据");
        }
        tAppService.removeBatch(idList);
        return Rsp.success();
    }

    @GetMapping("getById")
    @ApiOperation("根据组件查询")
    public Rsp getById(Integer id){
        return Rsp.success(tAppService.getAppById(id));
    }


    @GetMapping("getDetail")
    @ApiOperation("查询详情")
    public Rsp  getDetail(Integer id){
        return Rsp.success(tAppService.getDetail(id));
    }


    @GetMapping("listAppServer")
    @ApiOperation("获取应用服务器")
    public Rsp listAppServer(Integer id){
        return Rsp.success(tAppService.listAppServerVo(id));
    }


    @GetMapping("stopCmd")
    @ApiOperation("停止")
    public Rsp stopCmd(String ids){
        List<Integer> idList = new ArrayList<>();
        for(String id : ids.split(",")){
            if(StringUtils.isNotEmpty(id)){
                idList.add(Integer.valueOf(id));
            }
        }
        return Rsp.success(tAppService.stopCmd(idList));
    }

    @GetMapping("startCmd")
    @ApiOperation("停止")
    public Rsp startCmd(String ids){
        List<Integer> idList = new ArrayList<>();
        for(String id : ids.split(",")){
            if(StringUtils.isNotEmpty(id)){
                idList.add(Integer.valueOf(id));
            }
        }
        return Rsp.success(tAppService.startCmd(idList));
    }

    @GetMapping("restartCmd")
    @ApiOperation("停止")
    public Rsp restartCmd(String ids){
        List<Integer> idList = new ArrayList<>();
        for(String id : ids.split(",")){
            if(StringUtils.isNotEmpty(id)){
                idList.add(Integer.valueOf(id));
            }
        }
        return Rsp.success(tAppService.reStart(idList));
    }

    @GetMapping("downLog")
    public void   downLog(String ids,String nextPath, HttpServletResponse response){
        List<Integer> idList = new ArrayList<>();
        for(String id : ids.split(",")){
            if(StringUtils.isNotEmpty(id)){
                idList.add(Integer.valueOf(id));
            }
        }
        tAppService.downLog(idList,nextPath,response);
    }




}
